package com.game.sprite.components;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.game.sprite.BasicSprite;

/**
* @author oven:
* @version 创建时间：2012-11-30 上午11:20:45
* list
*/
public class BasicList extends BasicSprite {
	public static final byte DIRECTION_LANDSCAPE=0;
	public static final byte DIRECTION_PORTRAIT=1;
	protected byte direction = 0;//0:横向 1: 纵向
	protected int margin_x=0;//横向外边距
	protected int margin_y=0;//纵向外边距
	
	protected int scroolValue=0;//动值  负数
	
	protected float scroolBarSize=10;//滚动条厚度
	
	protected float totalChildWidth=0;
	protected float totalChildHeight=0;
	
	//拖动处理
	protected GestureDetector gd;
	
	protected BasicListData<BasicListItem> data;
	
	public BasicList(BasicListData<BasicListItem> data,int x,int y,int width,int height){
		//初始化位置
		basicRegion.setPostx(x);
		basicRegion.setPosty(y);
		//初始化宽高
		basicRegion.setWidth(width);
		basicRegion.setHeight(height);
		//初始化数据集
		this.data=data;
		
//		//不绘制区域
		drawCollectionRegion=false;
		drawRegion=false;
	}
	
	@Override
	protected  void paint(Canvas canvas) {
		canvas.save();
		float startx = getPostX();
		float starty = getPostY();
		//移动到list的开始位置
		canvas.translate(startx, starty);
		
		float listWidth=getWidth();
		float listHeight=getHeight();
		
		int count = data.size();
		switch (direction) {
		case DIRECTION_LANDSCAPE:{//横
			//只显示list的区域(两种方式)
			canvas.clipRect(0, 0, listWidth,listHeight+scroolBarSize);
			float childStartX=0;
			float childEndX=0;
			float totalChildWidth=0;
			for(int i=0;i<count;i++){
				BasicListItem item = data.get(i);
				childStartX=totalChildWidth+scroolValue;//child的开始位置
				totalChildWidth=totalChildWidth+margin_x+item.getWidth();//计算总长
				childEndX=totalChildWidth+scroolValue;//child的结束位置
				if(childEndX>=0&&childStartX<=listWidth){//在显示区域内则绘制
					canvas.save();
					//移动到开始位置
					canvas.translate(childStartX, 0);
					//只显示item大小
					canvas.clipRect(0,0,item.getWidth(),item.getHeight());
					//绘制item背景
					canvas.drawColor(Color.WHITE);
					//绘制item
					item.paint(canvas,paint);
					canvas.restore();
				}
			}
			
			//有溢出
			if(totalChildWidth>listWidth){
				//绘制滑动条
				//先绘制背景
				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				float sx = 0; //相对list的位置
				float sy =  listHeight;
				RectF background = new RectF(sx,sy,listWidth,scroolBarSize+sy);
				canvas.drawRoundRect(background,scroolBarSize/2,scroolBarSize/2, paint);
				//绘制bar
				paint.setColor(Color.RED);
				//滚动条长度  按比例计算
				float scroolWidth = listWidth*listWidth/totalChildWidth;
				//滚动条移动的长度  按比例计算
				sx=Math.abs(scroolValue)*listWidth/totalChildWidth;
				RectF scrool = new RectF(sx,sy,sx+scroolWidth,scroolBarSize+sy);
				canvas.drawRoundRect(scrool,scroolBarSize/2,scroolBarSize/2, paint);
			}
			this.totalChildWidth=totalChildWidth;
			break;
		}
		
		case DIRECTION_PORTRAIT:{//竖
			canvas.clipRect(0, 0, listWidth+scroolBarSize,listHeight);
			float childStartY=0;
			float childEndY=0;
			float totalChildHeight=0;
			for(int i=0;i<count;i++){
				BasicListItem item = data.get(i);
				childStartY=totalChildHeight+scroolValue;//child的开始位置
				totalChildHeight=totalChildHeight+margin_y+item.getHeight();//计算总长
				childEndY=totalChildHeight+scroolValue;//child的结束位置
				if(childEndY>=0&&childStartY<=listHeight){//在显示区域内则绘制
					canvas.save();
					//移动到开始位置
					canvas.translate(0,childStartY);
					//只显示item大小
					canvas.clipRect(0,0,item.getWidth(),item.getHeight());
					//绘制item背景
					canvas.drawColor(Color.WHITE);
					//绘制item
					item.paint(canvas,paint);
					canvas.restore();
				}
			}
			
			//有溢出
			if(totalChildHeight>listHeight){
				//绘制滑动条
				//先绘制背景
				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				float sx = listWidth; //相对list的位置
				float sy = 0;
				RectF background = new RectF(sx,sy,scroolBarSize+sx,listHeight);
				canvas.drawRoundRect(background,scroolBarSize/2,scroolBarSize/2, paint);
				//绘制bar
				paint.setColor(Color.RED);
				//滚动条长度  按比例计算
				float scroolHeight = listHeight*listHeight/totalChildHeight;
				//滚动条移动的长度  按比例计算
				sy=Math.abs(scroolValue)*listHeight/totalChildHeight;
				RectF scrool = new RectF(sx,sy,scroolBarSize+sx,sy+scroolHeight);
				canvas.drawRoundRect(scrool,scroolBarSize/2,scroolBarSize/2, paint);
			}
			this.totalChildHeight=totalChildHeight;
			break;
		}
			
		default:
			break;
		}
		
	    canvas.restore();
	}

	
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		switch (direction) {
		case DIRECTION_LANDSCAPE:{
			float listWidth = getWidth();
			if((totalChildWidth>listWidth&&Math.abs(scroolValue)<totalChildWidth-listWidth&&distanceX>0)||(scroolValue<0&&distanceX<0)){
				//左边正,右边负
				scroolValue=(int) (scroolValue-distanceX);
				//控制临界值
				if(scroolValue>0){
					scroolValue=0;
				}else if(Math.abs(scroolValue)>totalChildWidth-listWidth){
					scroolValue=(int) (listWidth-totalChildWidth);
				}
				return true;
			}
		}
		case DIRECTION_PORTRAIT:{
			float listHeight = getHeight();
			if((totalChildHeight>listHeight&&Math.abs(scroolValue)<totalChildHeight-listHeight&&distanceY>0)||(scroolValue<0&&distanceY<0)){
				//左边正,右边负
				scroolValue=(int) (scroolValue-distanceY);
				//控制临界值
				if(scroolValue>0){
					scroolValue=0;
				}else if(Math.abs(scroolValue)>totalChildHeight-listHeight){
					scroolValue=(int) (listHeight-totalChildHeight);
				}
				return true;
			}
		}	

		default:
			break;
		}
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	public int getMargin_x() {
		return margin_x;
	}

	public int getMargin_y() {
		return margin_y;
	}

	public void setMargin_x(int margin_x) {
		this.margin_x = margin_x;
	}

	public void setMargin_y(int margin_y) {
		this.margin_y = margin_y;
	}

	public byte getDirection() {
		return direction;
	}

	public void setDirection(byte direction) {
		this.direction = direction;
	}

}
